
import { getUserInfo } from '~/api/account'


export const useUser = defineStore(
  'user',
  () => {
    const token = ref('');
    const isLogin = ref(false);
    const personalInfo = ref({
      id: 0,
      username: '',
      head_img: '',
      phone: '',
      pwd: '',
      position: null,
      slogan: '',
      sex: '1',
      city: null,
      learn_time: null,
      openid: null,
    })
    //  切换登录状态
    const switchLoginState = (_token: string) => {
      token.value = _token;
      asyncUserInfo()
      
    };

    // 同步登录信息
    const asyncUserInfo = async () => {
      if (token.value === '') {
        isLogin.value = false
        return
      }
      const userInfo = (await getUserInfo())
      if (userInfo.code === 0) {
        isLogin.value = true
        personalInfo.value = { ...userInfo.data }
      }
    }

     // 退出登录
     const logout =  ()=> {
      clearInfo()
      navigateTo('/')
      message.success('退出登录成功！')
    }
    /**
     * 清空用户信息
     */
    const clearInfo = function () {
      token.value = ''
      isLogin.value = false
      personalInfo.value = {} as any
      // nextTick(() => navigateTo('/'))
    }

    return {
      switchLoginState,
      token,
      isLogin,
      personalInfo,
      asyncUserInfo,
      logout
    };
  },
  //持久化储存
  { persist: true }
);

